<template>
 <van-picker
    title="更改性别"
    show-toolbar
    :columns="columns"
    :default-index="value"
    @confirm="onConfirm"
    @cancel="$emit('closeEdit')"
  />
</template>

<script>
import { updataUserData } from '@/api/user'

export default {
  props: {
    value: {
      required: true,
      type: Number
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },

  created () {

  },

  methods: {
    async onConfirm (val, i) {
      this.$toast.loading({
        message: '修改中...',
        forbidClick: true
      })
      if (i === this.value) {
        return this.$emit('closeEdit')
      }
      try {
        await updataUserData({ gender: i })
        this.$emit('closeEdit')
        this.$emit('input', i)
        this.$toast('修改成功')
      } catch (err) {
        this.$toast('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
